#include "stdafx.h"
#include "MyView.h"
#include "MyModel.h"

MyView::MyView(Model * const inModel , HWND hWnd) : ViewGDI( inModel , hWnd )
{
}


MyView::~MyView(void)
{
}

void MyView::OnRender()
{
	MyModel * const pModel = dynamic_cast< MyModel * const >( this->getModel() );
	TCHAR str[ 256 ];

	switch( pModel->GetState() )
	{
	case eCONNECTING			: this->RenderConnecting(); break;
	case eWAITINGFORMATCHING	: this->RenderWaiting	(); break;
	case eGAMEPLAYING			: this->RenderGame		(); break;
	case eDISCONNECTED			: this->RenderError		(); break;
	}
	
	swprintf( str , 256 , _T( "%.0f , %.0f" ) , pModel->mLastMouseX , pModel->mLastMouseY ); 

	this->SetTxtColor( RGB( 255 , 0 , 0 ) );
	this->TxtOut( 0 , 12 , str );

	this->RenderTraces();	
}

void MyView::RenderConnecting	()
{
	TCHAR str[ 256 ];

	this->SetTxtColor( RGB( 0 , 0 , 255 ) );

	this->DrawTxt( 0 , 100 , this->GetWidth() , 120 , _T( "Connecting Server" ) );

	switch( ( GetTickCount() / 500 ) % 3 )
	{
	case 0:	swprintf( str , 256 , _T( "." ) ); break;
	case 1:	swprintf( str , 256 , _T( ".." ) ); break;
	case 2:	swprintf( str , 256 , _T( "..." ) ); break;
	}

	this->DrawTxt( 0 , 120 , this->GetWidth() , 140 , str );
}

void MyView::RenderWaiting		()
{
	// show waiting UI, and run ping from here.
	TCHAR str[ 256 ];

	this->SetTxtColor( RGB( 0 , 255 , 0 ) );

	this->DrawTxt( 0 , 100 , this->GetWidth() , 120 , _T( "Waiting for Matching..." ) );

	switch( ( GetTickCount() / 500 ) % 3 )
	{
	case 0:	swprintf( str , 256 , _T( "." ) ); break;
	case 1:	swprintf( str , 256 , _T( ".." ) ); break;
	case 2:	swprintf( str , 256 , _T( "..." ) ); break;
	}

	this->DrawTxt( 0 , 120 , this->GetWidth() , 140 , str );
}

void MyView::RenderGame			()
{
	// Initialize game objects and switch to game screen.
}

void MyView::RenderError		()
{
	// close the connection and show error message.
}

void MyView::RenderTraces		()
{
	// pressing shift key will trigger trace log
	if( GetAsyncKeyState( VK_SHIFT ) )
	{
		MyModel * const pModel = dynamic_cast< MyModel * const >( this->getModel() );

		std::list< std::wstring > list = pModel->GetTraces();

		this->SetTxtColor( RGB( 255 , 0 , 0 ) );

		this->SetBkMode( OPAQUE );
		this->SetBkColor( RGB( 0 , 0 , 0 ) );

		int i = 0 ;
		int dy = 20;
		for( std::list< std::wstring >::iterator list_iter = list.begin(); 
			list_iter != list.end(); list_iter++)
		{
			std::wstring str = *list_iter;
			this->TxtOut( 0 , this->GetHeight() - dy * ( ++i ) , str.c_str() );
		}

		this->SetBkMode( TRANSPARENT );
	}
}